package com.weitu.permission.dictionary.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.dictionary.entity.Dictionary;
import com.weitu.permission.dictionary.service.DictionaryService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/3/14.
 */
@Controller
@RequestMapping(value = "permission/dictionary")
public class DictionaryController extends BaseController{
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"permission/dictionary";
    private final DictionaryService dictionaryService;

    @Autowired
    public DictionaryController(DictionaryService dictionaryService){
        this.dictionaryService=dictionaryService;
    }

    /**
     * 跳入字典页面
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return MODULE_BATH_PATH + "/list";
    }

    /**
     * 获取字典页面数据列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request) throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        int count=this.dictionaryService.queryCount(paramMap);

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);

        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);
        List<Dictionary> dataList = this.dictionaryService.queryDictionaryListPage(paramMap);//获取列表数据

        return dataGridPagination.flushDataGridData(dataList);
    }

    /**
     * 字典判断字典类型是否可删除
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDictionaryTypeJudgement",method = RequestMethod.POST)
    public AjaxResult deleteDictionaryTypeJudgement(HttpServletRequest request) throws Exception{
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        int count=this.dictionaryService.queryCount(paramMap);
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("该类型下有字典不可以删除！");
            result.setSuccess(false);
            return result;
        }else {
            result.setMessage("该类型下没有没有字典可以删除！");
            result.setSuccess(true);
            return result;
        }
    }
    /**
     * 调到字典添加窗口
     * @return
     */
    @RequestMapping(value = "/addDictionary",method = RequestMethod.GET)
    public String addDictionary(){
        return MODULE_BATH_PATH+"/add";
    }

    /**
     * 字典添加
     * @param dictionary
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addDictionary",method = RequestMethod.POST)
    @SystemLog(module = "字典管理",methods = "添加字典")
    public AjaxResult addDictionaryAjax(Dictionary dictionary){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        AjaxResult result = new AjaxResult();

        putParametersIntoMap(paramMap);

        dictionary.setId(IDGenerator.getId());
        dictionary.setAddTime(DateTimeUtil.formatterDateTime());
        dictionary.setAddUserId(BaseController.getSessionUser().getId());
        dictionary.setDelState(0);
        int count = this.dictionaryService.insertDictionary(dictionary);//受影响的行数

        if (count>0){
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }
    /**
     * 调到字典编辑窗口
     * @return
     */
    @RequestMapping(value = "/editDictionary",method = RequestMethod.GET)
    public ModelAndView editDictionary(String id){

        Dictionary dictionary=dictionaryService.queryDictionaryById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("dictionary", dictionary);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");

        return modelAndView;
    }

    /**
     * 根据id查详情
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDictionaryById",method = RequestMethod.POST)
    public Dictionary queryDictionaryById(String id){
        Dictionary dictionary=dictionaryService.queryDictionaryById(id);
        return dictionary;
    }
    /**
     * 编辑字典
     * @param dictionary
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDictionary",method = RequestMethod.POST)
    @SystemLog(module = "字典管理",methods = "编辑字典")
    public AjaxResult editDictionaryAjax(Dictionary dictionary){
        Map<String, Object> paramMap = new HashMap<String, Object>();

        putParametersIntoMap(paramMap);
        dictionary.setUpdateTime(DateTimeUtil.formatterDateTime());
        dictionary.setUpdateUserId(BaseController.getSessionUser().getId());
        int count = this.dictionaryService.updateDictionary(dictionary);//受影响的行数
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("编辑成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("编辑失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 删除字典
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDictionary",method = RequestMethod.POST)
    @SystemLog(module = "字典管理",methods = "删除字典")
    public AjaxResult deleteDictionary(String id){
        int boo=dictionaryService.deleteDictionary(id);
        AjaxResult result = new AjaxResult();
        if(boo>=1){
            result.setMessage("删除成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("删除失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 根据字典类型查出所有字典id和名称
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDictionaryByDictionaryTypeCode",method = RequestMethod.POST)
    public List<Map<String,Object>> queryDictionaryByDictionaryTypeCode(String code){
        return this.dictionaryService.queryDictionaryByDictionaryTypeCode(code);
    }

    /**
     * 根据扩展字段查列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDictionaryByDatas",method = RequestMethod.POST)
    public List<Map<String,Object>> queryDictionaryByDatas(HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        putParametersIntoMap(map);
        return this.dictionaryService.queryDictionaryByDatas(map);
    }
    /**
     * 添加编辑判断code
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAndAlterDictionaryJudgement",method = RequestMethod.POST)
    public AjaxResult addAndAlterDictionaryJudgement(String code){
        int count=this.dictionaryService.queryCountByCode(code);
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("标识重复，保存失败！");
            result.setSuccess(false);
            return result;
        }else {
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }
    }
}
